package ioperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-24
 * Time: 17:07
 */
public class DeleteOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println();
        System.out.println("=====删除图书=====");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您要删除图书的书名：");
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        if(currentSize == 0) {
            System.out.println("本图书管理系统中未存储任何图书，无法进行删除操作！！！");
            System.out.println();
            return;
        }
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                for (int j = i; j < currentSize - 1; j++) {
                    Book book1 = bookList.getBook(j + 1);
                    bookList.setBook(j, book1);
                }
                bookList.setBook(currentSize - 1,null);
                bookList.setUsedSize(currentSize - 1);
                System.out.println("成功删除该图书！！！");
                System.out.println();
                return;
            }
        }
        System.out.println("没有查找到该图书！！！");
        System.out.println();
    }
}
